﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BaseHelper;
using System.Drawing;
using System.Text.RegularExpressions;
using DeviceSDK.LED.XianKe.XianKe;
using BaseHelper.Base;


namespace DeviceSDK.LED.XianKe
{
    public class XianKe_RXTX
    {
        private static SortedList<string, string> slPlaybill000 = new SortedList<string, string>();
        private static SortedList<string, string> slLastMd5 = new SortedList<string, string>();
 
        //获取错误代码
        public static string Get_Error(int ret)
        {
            switch (ret)
            {
                case 0:
                    return "操作成功！";

                case 1:
                    return "操作失败！";

                case 2:
                    return "发送节目单失败！";

                case 3:
                    return "保存节目单到磁盘失败！";

                case 4:
                    return "有素材文件格式不支持！";

                case 5:
                    return "更新图片素材999.bmp失败！";

                case -1:
                    return "操作失败！";

                default:
                    return "未知错误！";
            }
        }



        public static RXTX_SDK Connect(string IP, int Port)
        {

            RXTX_SDK rxtx = new RXTX_SDK();
            if (rxtx.Connect(IP, Port))
            {
                return rxtx;
            }
            return null;
        }


        //重启
        public static int Set_Reboot(string IP,int Port)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;

            int ret = rxtx.Reboot() ? 0 : 1;

            return ret;
        }

        //获得-屏幕状态
        public static int Get_Screen_Status(string IP,int Port,ref int Value)
        {
            //RXTX_SDK rxtx = Connect(IP);
            //if (rxtx == null)
            //    return 1;

            return -1;
        }

        //设置-屏幕-关闭
        public static int Set_Screen_Close(string IP,int Port)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;

            int ret = rxtx.SetScreenState(false) ? 0 : 1;

            rxtx.Close();
            return ret;

        }

        //设置-屏幕-开启
        public static int Set_Screen_Open(string IP, int Port)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;

            int ret = rxtx.SetScreenState(true) ? 0 : 1;

            rxtx.Close();
            return ret;
        }

        //获得-屏幕亮度
        public static int Get_Brightness(string IP,int Port, ref int Value)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;

            Value = rxtx.GetBrightness();

            rxtx.Close();
            int ret = Value == -1 ? 1 : 0;

            return ret;
        }

        //设置-屏幕亮度
        public static int Set_Brightness(string IP, int Port, int Value)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;

            int ret = rxtx.SetScreenLight(Value) ? 0 : 1;

            rxtx.Close();

            return ret;
        }

        //设置-时间
        public static int Set_Time(string IP,int Port)
        {
            RXTX_SDK rxtx = Connect(IP,Port);
            if (rxtx == null)
                return 1;

            int ret = rxtx.SetSystemTime() ? 0 : 1;

            rxtx.Close();
            return ret;
        }

        public static int Get_PlaybillContent(string IP, int Port,ref string Content)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;
            Content=rxtx.GetPlaybillContent();
            
            rxtx.Close();
            return Content==null?1:0;
        }


        public static int Set_PlaybillID(string IP, int Port, string ID)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;
            bool ret = false;
            int playbillID = 0;
            if (int.TryParse(ID, out playbillID))
            {
                ret = rxtx.SetPlaybill(playbillID);
            }
            rxtx.Close();
            return ret ? 0 : 1;
        }

        public static int Get_File(string IP, int Port, string RemotePath,ref byte[] FileBody)
        {

            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;

            FileBody = rxtx.Download(RemotePath);

            rxtx.Close();
            return FileBody != null ? 0 : 1;
        }
    

        public static int Send_File(string IP, int Port, string RemotePath,string FilePath)
        {
            if (!File.Exists(FilePath))
                return 1;
            
            RXTX_SDK rxtx = Connect(IP, Port);
            if (rxtx == null)
                return 1;

            byte[] fileBody=File.ReadAllBytes(FilePath);
            bool ret= rxtx.UpLoad(RemotePath,fileBody);

            rxtx.Close();
            return ret ? 0 : 1;
        }
    
        //发送-节目
        public static LEDSendResult Send_Programs(bool IsTempPlaybill, string IP, int Port, int Width, int Height, PlaybillProgramInfo[] Programs,bool isCheckResult)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            LEDSendResult result = new LEDSendResult(isCheckResult);

            if (rxtx == null)
            {
                result.RetCode = 1;
                return result;
            }

            int ret = 1;
            PlayBill pb = new PlayBill();


            foreach (PlaybillProgramInfo pinfo in Programs)
            {

                if (pinfo.Type == "picture" && !pinfo.FilePath.ToString().Contains(".bmp"))
                {
                    try
                    {
                        FileInfo finfo = new FileInfo(pinfo.FilePath);
                        string newPath = finfo.FullName.Replace(finfo.Extension, ".bmp");

                        if (ImageHelper.Bmp32To24(finfo.FullName, newPath))
                            pinfo.FilePath = newPath;
                        else
                            pinfo.FilePath = "";
                        
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Err("DeviceSDK.LED.XianKe.XianKe_RXTX.cs", "Send_Programs()", ex);
                    }
                }
            }

  
            foreach (PlaybillProgramInfo pinfo in Programs)
            {

                switch (pinfo.Type)
                {
                    case "picture":
                        pb.AddImage(pinfo.FilePath,pinfo.PlayTime,pinfo.X,pinfo.Y);
                        break;

                    case "text":
                        pb.AddString(pinfo.Text, pinfo.PlayTime, pinfo.FontName, pinfo.FontSize, pinfo.FontColor, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height,pinfo.PositionHor,pinfo.PositionVer,"#000000",pinfo.IsEffectRand==0?1:0);
                        break;
                }
            }
            
            int playbillId = 999;// IsTempPlaybill ? 999 : 0;
            
            if (rxtx.SendPlaybill(playbillId, pb))
            {
                if (rxtx.SetPlaybill(playbillId))
                {
                    if (isCheckResult)
                    {
                        result.SendContent = pb.Create();
                        result.ReplayContent = rxtx.GetPlaybillContent(playbillId);
                    }

                    ret = 0;
                }
                else
                    ret = 1;
            }
            else
                ret = 2;

            result.RetCode = ret;
            rxtx.Close();

            return result;

        }
    }
}
